package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IQsgStoreService;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.Certificate;
import com.zscat.mallplus.qsg.CertificateService;
import com.zscat.mallplus.qsg.QsgStore;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.DistanceUtils;
import com.zscat.mallplus.utils.UserUtils;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 内部券管理Controller
 */
@RestController
@Api(tags = "CertificateController", description = "内部券管理")
@RequestMapping("/api/certificate")
public class CertificateController {

    @Autowired
    private CertificateService certificateService;
    @Autowired
    private IQsgStoreService storeService;
    @Autowired
    private IUmsMemberService memberService;

    @IgnoreAuth
    @GetMapping(value = "/store/getStoreById")
    @ApiOperation(value = "根据id查询套餐")
    public Object getStoreById(Long storeId) {
        return null;
    }

    @IgnoreAuth
    @GetMapping(value = "/list")
    @ApiOperation(value = "内部券列表")
    public Object list() {
        try {
            UmsMember member = UserUtils.getCurrentMember();
            QueryWrapper qw = new QueryWrapper();
            qw.eq("member_id",member.getId());
            qw.orderByAsc("certificate_status");
            List<Certificate> certificates = certificateService.list(qw);
            for (Certificate certificate : certificates){
                QsgStore store = storeService.getById(certificate.getStoreId());
                if (null!=store){
                    certificate.setStoreName(store.getName());
                }
            }
            return new CommonResult().success(certificates);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @SysLog(MODULE = "qsg", REMARK = "根据条件查询所有券列表")
    @ApiOperation("根据条件查询所有券列表")
    @GetMapping(value = "/listadmin")
    public Object getCertByPage(@RequestParam(required = false) String phone, @RequestParam(required = false) Long storeId,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize
    ) {
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("create_datetime");
        qw.eq("store_id", storeId);
        qw.eq("certificate_status",0);
        if (StringUtils.isNotBlank(phone)) {
            qw.like("member_phone", phone);
        }
        try {
            IPage<Certificate> certificateIPage = certificateService.page(new Page<>(pageNum, pageSize), qw);
            return new CommonResult().success(certificateIPage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new CommonResult().failed();
    }

    @IgnoreAuth
    @GetMapping(value = "/listByStore")
    @ApiOperation(value = "可用内部券列表")
    public Object listByStore(Long storeId,int integral) {
        try {
            UmsMember member = UserUtils.getCurrentMember();
            QueryWrapper qw = new QueryWrapper();
            qw.eq("member_id",member.getId());
            qw.eq("store_id",storeId);
            qw.eq("certificate_status",0);
            qw.le("integral",integral);
            List<Certificate> certificates = certificateService.list(qw);
            for (Certificate certificate : certificates){
                QsgStore store = storeService.getById(certificate.getStoreId());
                if (null!=store){
                    certificate.setStoreName(store.getName());
                }
            }
            return new CommonResult().success(certificates);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @GetMapping(value = "/delete")
    public Object delete(@RequestParam Long id) {
        try {
            if (certificateService.removeById(id)) {
                return new CommonResult().success();
            }
        } catch (Exception e) {
            return new CommonResult().failed();
        }
        return new CommonResult().failed();
    }

    @GetMapping(value = "/sendcert")
    public Object sendcert(@RequestParam Integer memberId,
                           @RequestParam String certificateName,
                           @RequestParam String integral,
                           @RequestParam Integer storeId) {
        try {
            UmsMember member = memberService.getById(memberId);
            Certificate certificate = new Certificate();
            certificate.setStoreId(storeId);
            certificate.setCertificateStatus(0);
            certificate.setCreateDatetime(new Date());
            certificate.setSourceType(2);
            certificate.setSourceCode("");
            certificate.setMemberPhone(member.getPhone());
            certificate.setMemberId(memberId);
            certificate.setIntegral(Integer.valueOf(integral));
            certificate.setCertificateName(certificateName);
            certificateService.save(certificate);
            return new CommonResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }
}
